/**
 * 
 */
package game.kill.biz.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import game.kill.biz.MessageSendBiz;
import game.kill.enums.GameRecordTypeEnum;
import game.kill.enums.PlayerActionEnum;
import game.kill.enums.PlayerRoleEnum;
import game.kill.model.GameRecord;
import game.kill.model.GameRoomInfo;
import game.kill.model.PlayerAction;
import game.kill.model.RoomUser;
import game.kill.model.StepActionRecord;
import game.kill.model.User;
import game.kill.service.UserService;
import lombok.extern.slf4j.Slf4j;
import weixin.popular.api.MessageAPI;
import weixin.popular.bean.message.message.TextMessage;

/**
 * @author macbook
 *
 */
@Slf4j
@Service("messageSendBiz")
public class MessageSendBizImpl implements MessageSendBiz {

	private String access_token = "";
	
	@Resource
	private UserService userService;
	
	@Override
	public void sendMsgByPlayerAction(PlayerAction playerAction, StepActionRecord stepActionRecord,
			GameRoomInfo gameRoomInfo) {

		PlayerActionEnum action = playerAction.getActionEnum();
		RoomUser actionUser = playerAction.getRoomUser();
		PlayerRoleEnum role = playerAction.getRoleEnum();
		if(action == PlayerActionEnum.kill){
			List<RoomUser> teamMembers = gameRoomInfo.findTeamPlayserByRoleId(PlayerRoleEnum.killer.getCode());
			
			String content = "队友 "+actionUser.getNumber()+" 号玩家已表态：杀死"+stepActionRecord.getTarget()+"号";
			List<Long> userIds = Lists.newArrayList();
			for(RoomUser roomUser:teamMembers){
				userIds.add(roomUser.getUserId());
			}
			
			Map<Long,User> userIdMap = userService.getUserMapByIds(userIds);
			for(Long userId:userIds){
				User user = userIdMap.get(userId);
				TextMessage textMessage = new TextMessage(user.getWecharUid(),content);
				log.info("test msg send:"+user.getWecharUid()+"+"+content);
				//MessageAPI.messageCustomSend(access_token, textMessage);
				
			}
			
		}
		
		if(action == PlayerActionEnum.discuss){
			List<RoomUser> teamMembers = gameRoomInfo.findTeamPlayserByRoleId(role.getCode());
			
			String content = "队友 "+actionUser.getNumber()+" 号玩家说："+stepActionRecord.getTarget();
			List<Long> userIds = Lists.newArrayList();
			for(RoomUser roomUser:teamMembers){
				userIds.add(roomUser.getUserId());
			}
			
			Map<Long,User> userIdMap = userService.getUserMapByIds(userIds);
			for(Long userId:userIds){
				User user = userIdMap.get(userId);
				TextMessage textMessage = new TextMessage(user.getWecharUid(),content);
				System.out.println("test msg send:"+user.getWecharUid()+"+"+content);
				MessageAPI.messageCustomSend(access_token, textMessage);
				
			}
		}
		
		if(action == PlayerActionEnum.check){
			List<RoomUser> teamMembers = gameRoomInfo.findTeamPlayserByRoleId(PlayerRoleEnum.police.getCode());
			
			String content = "队友 "+actionUser.getNumber()+" 号玩家已表态：查验"+stepActionRecord.getTarget()+"号";
			List<Long> userIds = Lists.newArrayList();
			for(RoomUser roomUser:teamMembers){
				userIds.add(roomUser.getUserId());
			}
			
			Map<Long,User> userIdMap = userService.getUserMapByIds(userIds);
			for(Long userId:userIds){
				User user = userIdMap.get(userId);
				TextMessage textMessage = new TextMessage(user.getWecharUid(),content);
				log.info("test msg send:"+user.getWecharUid()+"+"+content);
				//MessageAPI.messageCustomSend(access_token, textMessage);
				
			}
		}
		
		if(action == PlayerActionEnum.speak){

			Collection<RoomUser> teamMembers = gameRoomInfo.getRoomUserMap().values();
			
			String content = "玩家"+actionUser.getNumber()+" 号发言："+stepActionRecord.getInfo();
			List<Long> userIds = Lists.newArrayList();
			for(RoomUser roomUser:teamMembers){
				userIds.add(roomUser.getUserId());
			}
			
			Map<Long,User> userIdMap = userService.getUserMapByIds(userIds);
			for(Long userId:userIds){
				User user = userIdMap.get(userId);
				TextMessage textMessage = new TextMessage(user.getWecharUid(),content);
				
				log.info("test msg send:"+user.getWecharUid()+"+"+content);
				//MessageAPI.messageCustomSend(access_token, textMessage);
				
			}
		}
		
		if(action == PlayerActionEnum.vote){
			//什么都不做，投票时间结束后才能公布结果
		}

	}

	@Override
	public void sendMsgByGameRecord(GameRecord gameRecord,GameRoomInfo gameRoomInfo) {
		
		Integer type = gameRecord.getType();
		
		GameRecordTypeEnum gameRecordType = GameRecordTypeEnum.getByCode(type);
		log.info("test game record msg send:gameRecord={}",gameRecord);
		
		if(gameRecordType == GameRecordTypeEnum.check){
			
		}
		
		else if(gameRecordType == GameRecordTypeEnum.kill_dead){
			
		}
		
		else if(gameRecordType == GameRecordTypeEnum.voted_dead){
			
		}
		
	}

}
